package com.langxin.erp.core.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.langxin.erp.model.UserInfo;
import com.langxin.erp.util.web.ActionUtils;

/**
 * <b>用户权限拦截器</b>
 * <p>
 *     拦截除首页，登陆外的所有请求，强制登陆
 */
public class UserInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

    	UserInfo user = ActionUtils.getCurrentUser();
    	if(user == null){
    		ActionUtils.getResponse().getWriter().write("<script>alert('退出重新登录');top.location.href='/';</script>");
    		return false;
    	}
    	return super.preHandle(request, response, handler);
    }

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}

    
}
